package com.data;

import android.graphics.Color;

import com.model.LineModel;
import com.model.ReferenceModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 全局数据
 * Created by White
 * Email：whitelaning@qq.com
 */
public class Common {
    //----用于滑动定位
    public static int newX = 0;
    public static int newY = 0;
    public static float oldX = 0;
    public static float oldY = 0;

    public static int staticX = 0;
    public static int staticY = 0;
    //---------------------------------------------------------------------------------------
    public static int type = 0;//0 身高；1 体重； 2头围
    public static int xOffsetNumber = 2;//x轴坐标偏移多少个点
    //---------------------------------------------------------------------------------------
    public static List<String> xScaleArrayLabel = new ArrayList<>(); //X轴刻度名称
    public static int xScaleColor = Color.rgb(25, 156, 240);//X轴颜色
    public static int xScaleEachLength = 300;//X轴每一段的长度
    public static int xBoundaryValue = 5;//X轴边界偏移量
    public static int xScaleTotalLength = 0;//X轴总长度
    //---------------------------------------------------------------------------------------
    public static int[] yScaleArrayLabelHeight = new int[]{};//Y轴刻度值
    public static float[] yScaleBeginsHeight;//Y轴刻度的y坐标

    public static int yScaleColor = Color.rgb(25, 156, 240);//Y轴颜色
    public static int yScaleEachLength = 200;//Y轴每一段的长度
    public static int yScaleTotalLength = 0;//y轴总长度
    //---------------------------------------------------------------------------------------
    public static List<MyData> LineDataSeries = new ArrayList<>();//绘图数据
    //---------------------------------------------------------------------------------------
    public static ReferenceModel mReferenceModel;//参考数据线的全局数据
    public static LineModel mLineModel;//用户数据的全局数据

    /**
     * 初始化y的坐标。先设置完数据在调用
     */
    public static void initYBeginsPoint() {
        Common.yScaleBeginsHeight = new float[Common.yScaleArrayLabelHeight.length];

        for (int i = 0; i < Common.yScaleArrayLabelHeight.length; i++) {
            if (i == 0) {
                Common.yScaleBeginsHeight[i] = Common.yScaleTotalLength;
            } else {
                Common.yScaleBeginsHeight[i] = Common.yScaleBeginsHeight[i - 1] - Common.yScaleEachLength;
            }
        }
    }

    /**
     * 得到点的Y坐标
     *
     * @param data 输入值
     * @param type 0身高，1体重，2头围
     * @return 对应Y坐标
     */
    public static float getYDataPoint(float data, int type) {
        //0，1，2的算法暂时是一致的，并非冗余代码，保留是为了方便后面修改
        switch (type) {
            case 0:
                for (int i = 1; i < Common.yScaleArrayLabelHeight.length; i++) {
                    if (data < Common.yScaleArrayLabelHeight[i]) {
                        return Common.yScaleBeginsHeight[i] + Common.yScaleEachLength * (Common.yScaleArrayLabelHeight[i] - data) / (Common.yScaleArrayLabelHeight[i] - Common.yScaleArrayLabelHeight[i - 1]);
                    }
                }
                break;
            case 1:
                for (int i = 1; i < Common.yScaleArrayLabelHeight.length; i++) {
                    if (data < Common.yScaleArrayLabelHeight[i]) {
                        return Common.yScaleBeginsHeight[i] + Common.yScaleEachLength * (Common.yScaleArrayLabelHeight[i] - data) / (Common.yScaleArrayLabelHeight[i] - Common.yScaleArrayLabelHeight[i - 1]);
                    }
                }
                break;
            case 2:
                for (int i = 1; i < Common.yScaleArrayLabelHeight.length; i++) {
                    if (data < Common.yScaleArrayLabelHeight[i]) {
                        return Common.yScaleBeginsHeight[i] + Common.yScaleEachLength * (Common.yScaleArrayLabelHeight[i] - data) / (Common.yScaleArrayLabelHeight[i] - Common.yScaleArrayLabelHeight[i - 1]);
                    }
                }
                break;
        }
        return 0;
    }

    public static int parentWidth; //LineView的父控件宽度
    public static int parentHeight; //LineView的父控件宽度

    public static void parseX(float x, int width, int height) {
        if (x < 0) {
            Common.newX = 0;
        } else if (x > width - parentWidth) {
            Common.newX = width - parentWidth;
        } else {
            Common.newX = (int) x;
        }

        switch (type) {
            case 0:
                Common.newY = Common.staticY - Common.newX * Common.yScaleTotalLength / (Common.xScaleTotalLength + Common.yScaleEachLength * 20);
                break;
            case 1:
                Common.newY = Common.staticY - Common.newX * Common.yScaleTotalLength / (Common.xScaleTotalLength + Common.yScaleEachLength * 120);
                break;
            case 2:
                Common.newY = Common.staticY - Common.newX * Common.yScaleTotalLength / (Common.xScaleTotalLength + Common.yScaleEachLength * 20);
                break;
        }


        if (Common.newY < 0) {
            Common.newY = 0;
        } else if (Common.newY > height - parentHeight) {
            Common.newY = height - parentHeight;
        } else {
            Common.newY = Common.newY;//知道无用，但是为了整体好看..
        }

        //初步防抖
        if (width <= parentWidth) {
            Common.newX = 0;
        }
        if (height <= parentHeight) {
            Common.newY = 0;
        }
    }
}
